package com.demo.util;

import java.time.*;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateUtils {

    private static ZoneOffset zo = OffsetDateTime.now().getOffset();

    public static Date toDate(LocalDateTime ld) {
        return new Date(ld.toInstant(zo).toEpochMilli());
    }

    public static Date toDate(LocalDate ld) {
        LocalDateTime localDateTime = ld.atStartOfDay(ZoneId.systemDefault()).toLocalDateTime();
        return new Date(localDateTime.toInstant(zo).toEpochMilli());
    }

    public static LocalDateTime toLocalDate(Date d) {
        OffsetDateTime offsetDateTime = d.toInstant().atOffset(zo);
        return offsetDateTime.toLocalDateTime();
    }

    /**
     * 获取某个日期的短周第一天
     *
     * @param date
     * @return
     */
    public static LocalDate getStartEta(LocalDate date) {
        LocalDate wld = getStartEtaLong(date);

        if (wld.getMonth() != date.getMonth()) {
            return date.with(TemporalAdjusters.firstDayOfMonth());
        } else {
            return wld;
        }
    }

    /**
     * 获取某个日期的短周第一天
     * 
     * @param date
     * @return
     */
    public static LocalDate getEndEta(LocalDate date) {
        LocalDate wld = getEndEtaLong(date);
        if (wld.getMonth() != date.getMonth()) {
            return date.with(TemporalAdjusters.lastDayOfMonth());
        } else {
            return wld;
        }
    }

    /**
     * 获取某个日期的长周第一天
     * 
     * @param date
     * @return
     */
    public static LocalDate getStartEtaLong(LocalDate date) {
        if (date.getDayOfWeek() == DayOfWeek.MONDAY) {
            return date;
        } else {
            return date.with(ChronoField.DAY_OF_WEEK, 1);
        }

    }

    /**
     * 获取某个日期的长周最后一天
     * 
     * @param date
     * @return
     */
    public static LocalDate getEndEtaLong(LocalDate date) {
        if (date.getDayOfWeek() == DayOfWeek.SUNDAY) {
            return date;
        } else {
            return date.with(ChronoField.DAY_OF_WEEK, 7);
        }

    }

    public static List<LocalDate> seperateDateLong(LocalDate start, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        LocalDate eta = getStartEtaLong(start);
        list.add(eta);
        boolean flag = true;
        while (flag) {
            eta = eta.plusWeeks(1);
            if (eta.isAfter(end)) {
                flag = false;
            } else {
                list.add(eta);
            }
        }
        return list;
    }

    public static List<LocalDate> seperateDate(LocalDate start, LocalDate end) {
        List<LocalDate> list = new ArrayList<>();
        LocalDate eta = getStartEta(start);
        LocalDate feta = getEndEta(start);
        list.add(eta);
        boolean flag = true;

        while (flag) {
            eta = feta.plusDays(1);
            feta = getEndEta(eta);
            if (eta.isAfter(end)) {
                flag = false;
            } else {
                list.add(eta);
            }
        }
        return list;
    }

}
